home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap06 / Triangle / Triangle.c next >
Encoding:
C/C++ Source or Header  |  1999-09-02  |  2.0 KB  |  98 lines

  1. // Triangle.c
  2. // OpenGL SuperBible, Chapter 4
  3. // Demonstrates OpenGL Primative GL_POINTS with point size
  4. // Program by Richard S. Wright Jr.
  5.  
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8. #include <gl/glu.h>
  9. #include <gl/glut.h>
  10. #include <math.h>
  11.  
  12.  
  13.  
  14. // Called to draw scene
  15. void RenderScene(void)
  16.     {
  17.     // Clear the window with current clearing color
  18.     glClear(GL_COLOR_BUFFER_BIT);
  19.  
  20.     // Enable smooth shading
  21.     glShadeModel(GL_SMOOTH);
  22.  
  23.     // Draw the triangle
  24.     glBegin(GL_TRIANGLES);
  25.         // Red Apex
  26.         glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0);
  27.         glVertex3f(0.0f,200.0f,0.0f);
  28.  
  29.         // Green on the right bottom corner
  30.         glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0);
  31.         glVertex3f(200.0f,-70.0f,0.0f);
  32.  
  33.         // Blue on the left bottom corner
  34.         glColor3ub((GLubyte)0,(GLubyte)0,(GLubyte)255);
  35.         glVertex3f(-200.0f, -70.0f, 0.0f);
  36.     glEnd();
  37.  
  38.     // Flush drawing commands
  39.     glutSwapBuffers();
  40.     }
  41.  
  42. // This function does any needed initialization on the rendering
  43. // context. 
  44. void SetupRC()
  45.     {
  46.     // Black background
  47.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  48.     }
  49.  
  50.  
  51. void ChangeSize(int w, int h)
  52.     {
  53.     GLfloat windowHeight,windowWidth;
  54.     
  55.     // Prevent a divide by zero, when window is too short
  56.     // (you cant make a window of zero width).
  57.     if(h == 0)
  58.         h = 1;
  59.  
  60.     // Set the viewport to be the entire window
  61.     glViewport(0, 0, w, h);
  62.  
  63.     // Reset the coordinate system before modifying
  64.     glLoadIdentity();
  65.  
  66.  
  67.     // Keep the square square.
  68.  
  69.     // Window is higher than wide
  70.     if (w <= h) 
  71.         {
  72.         windowHeight = 250.0f*h/w;
  73.         windowWidth = 250.0f;
  74.         }
  75.     else 
  76.         {
  77.         // Window is wider than high
  78.         windowWidth = 250.0f*w/h;
  79.         windowHeight = 250.0f;
  80.         }
  81.  
  82.     // Set the clipping volume
  83.     glOrtho(-windowWidth, windowWidth, -windowHeight, windowHeight, 1.0f, -1.0f);
  84.     }
  85.  
  86. int main(int argc, char* argv[])
  87.     {
  88.     glutInit(&argc, argv);
  89.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  90.     glutCreateWindow("RGB Triangle");
  91.     glutReshapeFunc(ChangeSize);
  92.     glutDisplayFunc(RenderScene);
  93.     SetupRC();
  94.     glutMainLoop();
  95.  
  96.     return 0;
  97.     }
  98.